/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageBean;

import SaveMessage.SavemessageService;
import UserBN.Authentication;
import UserBN.UserService;
import entity.House;
import entity.Savemessage;
import entity.SwhUser;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Mr-Quick
 */
@ManagedBean
@RequestScoped
public class MySaveMessage {

    public MySaveMessage() {
        getallsavemessage();
    }
    List<Savemessage> ls;
    SwhUser user;
    List<House> lh;
    int idsave;

    public int getIdsave() {
        return idsave;
    }

    public void setIdsave(int idsave) {
        this.idsave = idsave;
    }

    public List<Savemessage> getLs() {
        return ls;
    }

    public SwhUser getUser() {
        return user;
    }

    public void setUser(SwhUser user) {
        this.user = user;
    }

    public void getallsavemessage() {
        Authentication a = Authentication.getInstance();
        String account = a.getUsername();
        UserService us = new UserService();
        user = us.findAccountByUsername(account);
        SavemessageService sms = new SavemessageService();
        ls = sms.Findallmessagebyiduser(user);
        for (int i = 0; i < ls.size(); i++) {
            ls.get(i);
        }
    }

    public String deleteonemessage() {
        try {
            SavemessageService sms = new SavemessageService();
            Savemessage smss = sms.Findsavemessagebyidsavemessage(idsave);
            if (sms.deleteonesavemessage(smss).equalsIgnoreCase("true")) {
                return "Mysavemessages.xhtml?faces-redirect=true";
            }
        } catch (Exception ex) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "This Message had in your message", "This Message had in your message");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
                    return null;
    }
}
